package com.itcanteen.medical.docker.task;

import com.itcanteen.medical.docker.entity.Doctor;
import com.itcanteen.medical.docker.entity.Work;
import com.itcanteen.medical.docker.mapper.DoctorMapper;
import com.itcanteen.medical.docker.mapper.WorkMapper;
import com.itcanteen.medical.docker.util.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 任务调度（cron设置的为表达式，是执行的时间规则）
 *
 *
 */
@Component
public class SeckillTask {

    /**
     * 每秒中执行    固定的业务
     */
    @Autowired
    private DoctorMapper doctorMapper;
    @Autowired
    private WorkMapper workMapper;

    public static boolean flag = false;

    //五秒查询一次是否过期
    //@Scheduled(cron = "0/5 * * * * ?") // 间隔5秒执行
    public void refreshSeckillGoods() {

        //条件查询,type为专家医生
        Example example = new Example(Doctor.class);
        example.createCriteria().andEqualTo("type","专家门诊");
        //获取专家医生列表
        List<Doctor> doctors = doctorMapper.selectByExample(example);
        for (Doctor doctor : doctors) {
            findOneWork(doctor);
        }

    }

    //查询每一个上班时间点
    public void findOneWork(Doctor doctor){
        //条件查询,doctorId为专家医生的id,查询专家医生的每次上班信息
        Example example1 = new Example(Work.class);
        example1.createCriteria().andEqualTo("doctorId",doctor.getId());
        example1.createCriteria().andEqualTo("status",0);
        List<Work> works = workMapper.selectByExample(example1);
        for (Work work : works) {
            sendMessage(work,doctor.getName());
        }
    }

    //当前需要上班,给医生发短信
    public void sendMessage(Work work,String name){
        //当前时间
        Date date = new Date();
        //判断是否要上班
        if (work.getStartTime().getTime() <= date.getTime() && work.getEndTime().getTime() >= date.getTime()){
            System.out.println("需要打开上班------>" + name);
            if (!flag){
                SendSms.sendMag("15204178957","234");
                flag = true;
            }
        }
    }

}
